<!-- 首页 -->
<template>
	<view class="home-wrap u-m-b-20">
		<!-- systemBar: "", -->
		<!-- navigationBar: "", -->
		<u-popup v-model="showpopup" :round="20" mode="top" @close="close" @open="open">
			<view class="popup-box" :style="[{paddingTop:systemBar + 'px' }]">
				<view style="display: flex;justify-content: center;align-items: center;" :style="[{height: navigationBar + 'px' }]">
					<view style="position: fixed;left: 10px;">
						<u-icon name="arrow-left"></u-icon>
					</view>
					<view>智能排序</view>
				</view>
				<view>
					<view v-for="(item,index) in sortList" :key="index" class="check-right" :class="index==sortType?'active':''"
					 :data-index="index" @click="checkSortType">

						<view style="flex: 1;"> {{item.name}}</view>
						<view>
							<u-icon name="checkbox-mark" v-if="index==sortType" color="#FC5351"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<view v-if="isConnected && isRefresh" class="content-box">
			<!-- 导航栏 -->
			<shopro-navbar :isBack="false">
				<view class="u-flex-1 u-flex u-col-center" slot="content">
					<u-search shape="square" action-text="" v-model="keyword" @search="shopLists" :action-style="{'display':'none'}"
					 placeholder="请输入商品名称" :showAction="false" height="60" style="width: 90%;"></u-search>
				</view>
			</shopro-navbar>
			<view style="	padding-top: 0rpx;margin: 20rpx;border-radius: 16rpx;">
				<u-swiper :list="bannerList" @click="toJoinUs()" :height="300"></u-swiper>
			</view>
			<view style="margin: 20rpx;">
				<u-grid :border="false" col="5" >
					<u-grid-item v-for="(listItem,listIndex) in list" :key="listIndex" v-if="listIndex < 10"  @click="clickFood(listItem)">
						<image class="icon" :src="listItem.logo_image"></image>
						<!-- 	<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="listItem.name" :size="22"></u-icon> -->
						<text class="grid-text">{{listItem.name}}</text>
					</u-grid-item>
				</u-grid>
			</view>
			<!-- 	<u-sticky> -->
			<view id="id" class="filter" :style="[{top: stickyTop + 'px' }]" offset-top="200" custom-nav-height="220">
				<view :class="active=='-1'?'active':''" @click="checkFilter('-1')">全部
					<u-icon :name="active=='-1'?'/static/images/shangbang/arrow2.png':'/static/images/shangbang/arrow.png'" size="30"></u-icon>

				</view>
				<view :class="active=='5'?'active':''" @click="checkFilter('5')">附近<u-icon :name="active=='5'?'/static/images/shangbang/arrow2.png':'/static/images/shangbang/arrow.png'"
					 size="30"></u-icon>
				</view>
				<view :class="active=='0'?'active':''" @click="checkFilter('0')">{{sortList[sortType].name}}
					<u-icon :name="active=='0'?'/static/images/shangbang/arrow2.png':'/static/images/shangbang/arrow.png'" size="30"></u-icon>
				</view>
			</view>
			<!-- </u-sticky> -->
<!-- 			<view class="content">
 -->				<!-- 	商品 -->
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="content " @scrolltolower="lower"
				 >
				  <view v-for="(item,index) in shopList">
						<view style="padding: 10px;border-bottom: 1px solid #F3F3F3;display: flex;" :data-id="item.store_shop_id" @click="toItem">
							<view style="position: relative;">
								<image :src="item.shop_image" style="width: 260rpx;height: 194rpx;border-radius: 8rpx;"></image>
								<!-- <view style=" font-size: 12px;"> -->
								<view style="position: absolute;color: #FFFFFF; bottom: 10px; left: 5px;color: white;text-align: center; line-height: 18px;font-size: 12px;width: 68rpx;height: 36rpx;height: 18px;background: linear-gradient(270deg, #FF5251 0%, #FD8E8A 100%);border-radius: 8px 0px 8px 0px;opacity: 1;">
									{{item.discount}}
								</view>
							</view>
							<view>
								<view style="margin-left: 10px;">
									<view style="font-size: 32rpx;font-family: PingFang SC-Bold, PingFang SC;font-weight: bold;color: #333333;line-height: 50rpx;    white-space: nowrap;
    overflow: hidden;width: 400rpx;text-overflow: ellipsis;">{{item.shop_name}}</view>
									<view style="display: flex;flex-direction: row;">
										<view v-for="(tags,tagindex) in item.shop_tags" style="background-color: #FFF0E4;padding: 6rpx 10rpx;margin-right: 10rpx;border-radius: 2px 2px 2px 2px;font-size: 20rpx;font-family: PingFang SC-Medium, PingFang SC;font-weight: 500;color: #FE811C;line-height: 30rpx;">
											{{tags}}</view>
									</view>
									<view style="font-size: 20rpx;font-family: PingFang SC-Regular, PingFang SC;font-weight: 400;color: #999999;margin-top: 4px;">{{item.shop_text}}</view>
									<view style="left: -9rpx;position: relative;">

										<u-rate :value="item.shop_score" readonly activeColor="#FE811C"></u-rate>
									</view>
									<view v-if="item.quan_text!=''" style="display: flex;margin-top: 1px;margin: 0rpx 0rpx;">
										<!-- <image src="/static/images/shangbang/quan.png" style="width: 16px;height: 16.5px;"></image>
										<view class="lable" >{{item.quan_text}}</view> -->
										<view v-if="item.quan_text!=''">
											<image style="width: 32rpx;height: 32rpx;" src="../../static/images/shangbang/quan.png"></image>
										</view>
										<view v-if="item.quan_text!=''" class="lable">
											{{item.quan_text}}</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="current_page == last_page" style="height: 32px;font-size: 12px;font-family: PingFang SC-Regular, PingFang SC;font-weight: 400;color: #999999;line-height: 32px;text-align: center;">持续更新请期待~</view>

				</scroll-view>
		<!-- 	</view> -->
		</view>

	</view>
</template>

<script>
	// 获取系统状态栏的高度
	let systemInfo = uni.getSystemInfoSync();
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	import HuixuanClassify from "../../components/huixuan-classify/huixuan-classify";
	import HuixuanExercise from "../../components/huixuan-exercise/huixuan-exercise";
	export default {
		components: {},
		data() {
			return {
				current_page: 0,
				last_page: 1,
				sortType: 0,
				statusBarHeight: systemInfo.statusBarHeight,
				showpopup: false,
				active: "-1",
				themeColor: "#FC5351",
				// #ifdef MP-WEIXIN
				// #endif
				isRefresh: true,
				enable: false, //是否开启吸顶。
				isConnected: true, //是否有网
				showPrivacy: false, //协议
				isScorll: false,
				sortList: [{
						name: "智能排序",
						id: "1"
					},
					{
						name: "折扣高低",
						id: "2"
					},
					{
						name: "人均消费高低",
						id: "3"
					},
					{
						name: "好评优先",
						id: "4"
					},
					{
						name: "销量优先",
						id: "5"
					},
					{
						name: "距离优先",
						id: "6"
					},
				],
				bannerList: [
					"https://duomi.bigchun.com/images/banner.png"
				],
				list: [],
				systemBar: "",
				navigationBar: "",
				menu: "",
				page: 1,
				limit: 6,
				shop_sort: 0,
				shopList: [],
				stickyTop: 0,
				keyword: ""
			};
		},
		computed: {},
		onPullDownRefresh() {
			this.init();
      this.shopLists();
		},
		onPageScroll(e) {
			this.isScorll = e.scrollTop > 100 ? true : false;
		},
		onShow() {
			let that = this;
			this.enable = true;
			this.isLogin && this.getCartList();
			// 网络变化检测
			uni.onNetworkStatusChange(res => {
				this.isConnected = res.isConnected;
				res.isConnected && this.init();
			});
		},
		onHide() {
			this.enable = false;
		},
		onReachBottom(){
			this.lower();
		},
		onLoad() {
			wx.getSystemInfo({
				success: res => {
					this.system = res
				}
			})
			this.init();
			this.shopLists();
			//获取胶囊信息
			this.menu = wx.getMenuButtonBoundingClientRect()
			this.systemBar = this.system.statusBarHeight - 10 //状态栏高度
			this.navigationBar = (this.menu.top - this.system.statusBarHeight) * 2 + this.menu.height //胶囊高度
			this.stickyTop = this.systemBar + this.navigationBar + 5;
			console.log(this.systemBar);
			console.log(this.navigationBar);
			console.log('init');
			// #ifdef APP-VUE
			// app隐私协议弹窗

			if (!plus.runtime.isAgreePrivacy()) {
				this.showPrivacy = true;
				this.showNoticeModal = false;
			}

			// #endif
		},
		methods: {

			toupper() {
				 console.log("toupper" );
				const query = uni.createSelectorQuery().in(this);
				query.select('#id').boundingClientRect(data => {
				  console.log("得到布局位置信息" + JSON.stringify(data));
				  console.log("节点离页面顶部的距离为" + data.top);
				  uni.pageScrollTo({
				    scrollTop: data.top, // 滚动到的位置（距离顶部 px）
				    duration: 1000 //滚动所需时间 如果不需要滚动过渡动画，设为0（ms）
				  })
				}).exec();
			},

			async init() {

				console.log("=========initres")
				var that = this;
				that.isRefresh = false;
				that.$https('category.shangbangTop', {
						token: uni.getStorageSync("token"),
					})
					.then(res => {
						console.log("shangbangTop=========res", res)
						if (res.code === 1) {
              uni.stopPullDownRefresh();
              that.isRefresh = true;
							this.list = res.data.cate;
							var bannerList = res.data.lunbo;
							this.bannerList = []
							for (var i = 0; i < bannerList.length; i++) {
								var event = bannerList[i];
								var item = {}
								item.image = event.imageurl;
								item.title = event.title;
								item.id = event.id;
								this.bannerList.push(item)
							}
						}
					});

			},
			lower: function(e) {
				console.log(this.current_page)
				console.log(this.per_page)
				if (this.current_page != this.last_page) {
					this.page = this.page + 1;
					this.shopLists()
				}
			},
			onSearch: function(e) {
				console.log(this.current_page)
				console.log(this.per_page)
				this.page = 1,
				this.shopLists()
			},
			async shopLists() {
				console.log("=========initres")
				var that = this;
				//that.isRefresh = false;
				console.log("=========keyword", this.keyword)
				that.$https('category.shopLists', {
						token: uni.getStorageSync("token"),
						page: this.page,
						limit: this.limit,
						shop_sort: this.sortType,
						keyword: this.keyword,
						latitude: uni.getStorageSync("latitude"),
						longitude: uni.getStorageSync("longitude")
					})
					.then(res => {
						console.log("shopLists=========res", res)
						if (res.code === 1) {
							// that.shopList=res.data.data;
							if (res.data.current_page != 1) {
								that.shopList = that.shopList.concat(res.data.data);
								that.current_page = res.data.current_page;
								that.last_page = res.data.last_page;
								that.per_page = res.data.per_page;
								that.total = res.data.total;
							} else {
								that.shopList = res.data.data;
								that.current_page = res.data.current_page;
								that.last_page = res.data.last_page;
								that.per_page = res.data.per_page;
								that.total = res.data.total;
							}
						}
					});

			},
			toJoinUs() {
				uni.navigateTo({
					url: '/pages/app/joinUs/joinUs'
				})
			},
			toItem(e) {
				console.log(e.currentTarget.dataset.id)
				uni.navigateTo({
					url: '/pages/index/GoodsDetails?id=' + e.currentTarget.dataset.id
				})
			},
			clickFood(e) {
				console.log('e', e)
				if (e.name == "全部服务") {
					uni.navigateTo({
						url: '/pages/index/serve?type=category'
					})
				} else {
					uni.navigateTo({
						url: '/pages/index/food?store_cate_id=' + e.store_cate_id + "&title=" + e.name
					})
				}
			},
			open() {
				// console.log('open');
			},
			close() {
				this.showpopup = false
				// console.log('close');
			},
			checkFilter(active) {
				if (active == '0') {
					this.showpopup = true
					console.log(this.showpopup);
					this.active = active;
				} else {
					this.active = active;
					this.shopList = []
					this.shopLists()
				}

			},
			checkSortType(e) {
				var index = e.currentTarget.dataset.index;
				this.sortType = index;
				this.showpopup = false
				this.shopList = []
				this.shopLists()
				console.log(e);
			},
			result(val) {
				console.log('filter_result:' + JSON.stringify(val));
				this.filterResult = JSON.stringify(val, null, 2)
			},
			...mapActions(['appInit', 'getTemplate', 'getCartList']),
			// 初始化

		}
	};
</script>

<style lang="scss" scoped>
	/deep/.u-drawer-content {
		border-radius: 10px !important;
	}

	/deep/.u-drawer__scroll-view {
		border-radius: 10px !important;
	}

	/deep/.cu-bar .content {
		margin: auto 20rpx;
		width: 450rpx;

	}

	/deep/.u-grid-item {
		background: #f6f6f6 !important;
	}

	/deep/.uni-scroll-view {
		padding-top: 0px !important;
	}

	page {
		background-color: #fcfcfc !important;
	}

	.icon {
		width: 72rpx;
		height: 72rpx;
	}

	.grid-text {
		height: 32rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		line-height: 70rpx;
	}

	.content {
		margin: 20rpx;
		// margin-top: 0rpx!important;
		border-radius: 8px 8px 8px 8px !important;
		background-color: white;
		width: auto;
	}

	.filter {
		display: flex;
		align-items: center;
		flex-direction: row;
		position: sticky;
		// top: 10vh;
		background-color: white;
		z-index: 1000;

		// border-radius: 8px 8px 0px 0px;
		// margin: 0 20rpx;
		view {
			flex: 1;
			text-align: center;
			line-height: 70rpx;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #777777;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		u-icon {
			margin-left: 8rpx;
		}

		.active {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FC5351;
		}
	}

	.lable {
		border-left: transparent !important;
		border-radius: 0rpx 5rpx 5rpx 0rpx;
		left: -3rpx;
		padding: 0 5px;
		font-size: 10px;
		font-weight: 500;
		color: #FC5351;
		height: 32rpx;
		border: 1px solid #FC5351;
		display: flex;
		align-items: center;
		justify-content: center;
		align-content: center;
		position: relative;

	}

	.popup-box {
		padding-top: var(--status-bar-height);
		margin: 0 20rpx;
	}

	.nav-line {
		height: var(--window-top);
	}

	.check-right {
		height: 54px;
		font-size: 16px;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		line-height: 19px;
		align-items: center;
		display: flex;

		.active {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FC5351;
		}
	}

	.active {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FC5351;
	}

	.content {
		margin: 20rpx;
		border-radius: 8px 8px 8px 8px;
		background-color: white;
		// height: 100vh;
		// overflow: hidden;
	}
</style>
